package cn.psvmc.cxzapi.service.store;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.store.TAddressMapper;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.store.TAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 地址表 服务实现类
 * @Author auto
 * @Date 2024-08-15
 */
@Service
public class TAddressService extends AbstractService<TAddressMapper, TAddress> {

    @Resource
    private TAddressMapper tAddressMapper;

    /**
    * @Date auto 2024-08-15 新增地址表
    * @Param tAddress 地址表
    * @Return int
    */
    public int add(TAddress tAddress){
        return tAddressMapper.insert(tAddress);
    }

    /**
    * @Date auto 2024-08-15 更新地址表
    * @Param tAddress 地址表模型
    * @Return int
    */
    public int edit(TAddress tAddress) {
        // 通过id查询数据处理 详情
        TAddress originTAddress = this.detail(tAddress.getAddressid());
        if (originTAddress == null) {
            throw new ServiceException("未查询到对应的地址表信息！");
        }

        // 比较数据版本号
        if (!originTAddress.getRversion().equals(tAddress.getRversion())) {
            throw new ServiceException("数据已更新，请刷新后重试！");
        }

        // 字段为null 不更新
        return tAddressMapper.updateById(tAddress);
    }

    /**
    * @Date auto 2024-08-15 查询地址表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.model.store.TAddress
    */
    public TAddress detail(Integer id){
        return tAddressMapper.selectById(id);
    }

    /**
    * @Date auto 2024-08-15 删除地址表
    * @Param id 唯一id
    * @Param rversion 版本号
    * @Return int
    */
    public int delete(Integer id, Integer rversion){
        // 查询地址表详情
        TAddress tAddress = this.detail(id);
        if(tAddress == null){
            throw new ServiceException("未查询到对应的地址表信息！");
        }

        // 比较数据版本号
        if(!rversion.equals(tAddress.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }

        return tAddressMapper.deleteById(id);
    }

    /**
     * @Description 管理列表
     * @Author: YWX
     * @Date 2024/8/15 14:39
     **/
    public List<Map> listAll(SearchEntity entity) {
        return tAddressMapper.listAll(entity);
    }

    /**
     * @Description 选择列表
     * @Date 2024/8/15 14:39
     * @Author YWX
     * @Param [entity]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listForSel(SearchEntity entity) {
        return tAddressMapper.listForSel(entity);
    }
}
